package com.eightbears.beboy.ui.user.activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eightbears.beboy.R;
import com.eightbears.beboy.base.BaseActivity;
import com.eightbears.beboy.base.util.CommonUtils;
import com.eightbears.beboy.base.util.ErrorUtils;
import com.eightbears.beboy.base.util.GsonUtil;
import com.eightbears.beboy.base.util.SPUtil;
import com.eightbears.beboy.base.util.okhttp.OkHttpUtils;
import com.eightbears.beboy.base.util.okhttp.callback.StringCallback;
import com.eightbears.beboy.base.view.SwipeBackLayout;
import com.eightbears.beboy.entity.status.Status_Error;
import com.socks.library.KLog;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

import static com.eightbears.beboy.base.util.CommonAPI.KEY_HTTP_HEADER;
import static com.eightbears.beboy.base.util.CommonAPI.PAGE_SIZE;
import static com.eightbears.beboy.base.util.CommonAPI.URL_USER_CURRENTACCOUNT;

/**
 * 交易记录页面
 */
public class TransactionRecordActivity extends BaseActivity {


    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.tv_transaction_date)
    TextView tv_transaction_date;
    @BindView(R.id.tv_transaction_expend)
    TextView tv_transaction_expend;
    @BindView(R.id.tv_transaction_income)
    TextView tv_transaction_income;
    @BindView(R.id.iv_transaction_calender)
    ImageView iv_transaction_calender;
    @BindView(R.id.sr_transaction)
    SwipeRefreshLayout sr_transaction;
    @BindView(R.id.rv_transaction)
    RecyclerView rv_transaction;
    @BindView(R.id.sw)
    SwipeBackLayout sw;

    private String startTime;
    private String endTime;

    private boolean isErr = true;
    private int isEnd = 0;
//    private TransactionRecordAdapter mTransactionRecordAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_transaction_record);
        initView();
        initData();
        initEvent();
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
//        sr_transaction.setOnRefreshListener(this);
        sr_transaction.setProgressViewOffset(true, 0, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, getResources().getDisplayMetrics()));
        sr_transaction.setColorSchemeColors(getResources().getColor(R.color.text_select_color),
                getResources().getColor(R.color.text_select_color),
                getResources().getColor(R.color.text_select_color),
                getResources().getColor(R.color.text_select_color));
        tv_title.setText("交易记录");
        rv_transaction.setLayoutManager(new LinearLayoutManager(mContext));


    }

    @Override
    protected void initData() {
//        mTransactionRecordAdapter = new TransactionRecordAdapter();
//        mTransactionRecordAdapter.openLoadAnimation();
//        mTransactionRecordAdapter.setOnLoadMoreListener(this);
        //获取当前月时间戳()
        startTime = CommonUtils.getStartTimeOfMonth();
        endTime = CommonUtils.getEndTimeOfMonth();
        getData();
    }

    private void getData() {
        String USERTYPE = "1";
        int page = 1;
        OkHttpUtils.get().url(URL_USER_CURRENTACCOUNT + SPUtil.getUser().getData().getUser_id())
                .addHeader(KEY_HTTP_HEADER, "Bearer " + SPUtil.getAccessToken())
                .addParams("type", USERTYPE)
                .addParams("page", String.valueOf(page))
                .addParams("size", PAGE_SIZE)
                .addParams("start_at", startTime)
                .addParams("end_at", endTime)
                .build().execute(new StringCallback() {

            @Override
            public void onResponse(String response) {
                sr_transaction.setRefreshing(false);
                KLog.e(response + "请求成功");
                try {
//                    TransactionRecordEntity transactionRecordEntity = GsonUtil.fromJson(response, TransactionRecordEntity.class);
//                    //如果登录成功
//                    if (null != transactionRecordEntity && transactionRecordEntity.getStatus_code() == Integer.parseInt(CommonApi.CONNECTION_STATUS_SUCCESS)) {
//                        //获取商店优惠套餐
//                        isEnd = transactionRecordEntity.getData().getIs_end();
//                        List<TransactionRecordEntity.DataBean.ListBean> list = transactionRecordEntity.getData().getList();
//                        if (page == 1) {
//                            mTransactionRecordAdapter.setNewData(list);
//                        } else {
//                            mTransactionRecordAdapter.addData(list);
//                            mTransactionRecordAdapter.loadMoreComplete();
//                        }
//                        tv_transaction_expend.setText("支出 ￥" + String.valueOf(transactionRecordEntity.getData().getOutlay()));
//                        tv_transaction_income.setText("收入 ￥" + String.valueOf(transactionRecordEntity.getData().getIncome()));
//                        rv_transaction.setAdapter(mTransactionRecordAdapter);
//                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Call request, Exception e) {
                // 完成请求失败的各种错误提示
                KLog.e(e.getMessage());
                Status_Error status_error = GsonUtil.fromJson(e.getMessage(), Status_Error.class);
                ErrorUtils.alertError((Activity) mContext, status_error);
            }
        });
    }

    @Override
    protected void initEvent() {
        sw.setCallBack(new SwipeBackLayout.CallBack() {
            @Override
            public void onFinish() {
                finish();
            }
        });
    }

    @OnClick(R.id.ll_back)
    void back() {
        finish();
    }

//    @Override
//    public void onRefresh() {
//        mTransactionRecordAdapter.setEnableLoadMore(false);
//        new Handler().postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                isErr = true;
//                page = 1;
//                mTransactionRecordAdapter.getData().clear();
//                getData();
//                mTransactionRecordAdapter.setEnableLoadMore(true);
//            }
//        }, delayMillis);
//    }
//
//    @Override
//    public void onLoadMoreRequested() {
//        new Handler().postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                if (isEnd == 1) {
//                    mTransactionRecordAdapter.loadMoreEnd(true);
//                } else {
//                    if (isErr) {
//                        page++;
//                        getData();
//                    } else {
//                        isErr = true;
//                        T.getInstance().CenterShort(R.string.network_anomalies);
//                        mTransactionRecordAdapter.loadMoreFail();
//                    }
//                }
//            }
//        }, delayMillis);
//    }
}